package com.pro01.backend.mapper;

import com.pro01.backend.pojo.ShopItem;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ShopItemMapper {

    // 用户查看商品列表
    @Select("""
    SELECT *
    FROM shop_items
    WHERE status = 'ACTIVE'
    ORDER BY updated_at DESC
    LIMIT #{offset}, #{pageSize}
""")
    List<ShopItem> getActiveItemsPaged(@Param("offset") int offset, @Param("pageSize") int pageSize);

    @Select("SELECT COUNT(*) FROM shop_items WHERE status = 'ACTIVE'")
    int countActiveItems();

    // 查询单个商品
    @Select("SELECT * FROM shop_items WHERE id = #{itemId}")
    ShopItem getById(@Param("itemId") Long itemId);

    /**
     * 接下来是管理员接口
     */
    // 管理员添加商品
    @Insert("""
        INSERT INTO shop_items (name, description, type, points_required, extra_value)
        VALUES (#{name}, #{description}, #{type}, #{pointsRequired}, #{extraValue})
    """)
    int insertItem(ShopItem item);

    // 管理员下架商品
    @Update("""
    UPDATE shop_items
    SET status = 'DISABLED'
    WHERE id = #{itemId}
""")
    int disableItem(@Param("itemId") Long itemId);

    // 管理员重新上架商品
    @Update("""
    UPDATE shop_items
    SET status = 'ACTIVE'
    WHERE id = #{itemId}
""")
    int enableItem(@Param("itemId") Long itemId);

    // 管理员分页查看所有商品
    @Select("""
    SELECT *
    FROM shop_items
    ORDER BY updated_at DESC
    LIMIT #{offset}, #{pageSize}
""")
    List<ShopItem> getAllItemsPaged(@Param("offset") int offset, @Param("pageSize") int pageSize);

    @Select("SELECT COUNT(*) FROM shop_items")
    int countAllItems();


}

